# -*- coding: utf-8 -*-
__author__ = "Ypf"
_date_ = "2019/6/25-17:00"

from collections import defaultdict

from common.mssql_helper import g_msql_inst
from common.utils import format_float, create_uuid
from crm_data_report.config import xiaoshoujingli_dic, xsjl_list
from crm_data_report.cm_db import  filter_sb


class SalesManager(object):
    @staticmethod
    def get_tjjx(sql, name_dic):
        '''
        获取mingzi
        :param sql: 封装的sql语句
        :return:
        '''
        res_list = g_msql_inst.fetch_all(sql)
        # print(res_list)
        # return res_list
        # select  khqc, idowner from  account
        # 假设按照idowner进行group by
        tjjx_dic = defaultdict(list)
        for (xsgljx,gzzdr) in res_list:
            idowner = name_dic.get(gzzdr)
            tjjx_dic[idowner].append((format_float(xsgljx)))
        return filter_sb(tjjx_dic, xsjl_list)

    @staticmethod
    def get_bgjf(sql, name_dic):
        '''
        获取销售经理绩效
        :param sql: 封装的sql语句
        :return:
        '''
        res_list = g_msql_inst.fetch_all(sql)
        # print(res_list)
        # return res_list
        # select  khqc, idowner from  account
        # 假设按照idowner进行group by
        bgjf_dic = defaultdict(list)
        for (bgjf,idowner ) in res_list:
            idowner = name_dic.get(idowner)
            bgjf_dic[idowner].append((format_float(bgjf)))

        return filter_sb(bgjf_dic,xsjl_list)

    @staticmethod
    def sale_xsjl_statics(bgtime,edtime, input_dic,db_insert_sql):
        '''
        保存数据库信息
        :param report_list: 统计出的报表结果数据cpjlrckh
        :return:
        '''
        value_tuple_list = []
        for key in input_dic:
            id = create_uuid()
            dic_val = input_dic[key]
            value_tuple = (
            id,''.join(xiaoshoujingli_dic[key]), dic_val[0], dic_val[1], dic_val[2], dic_val[3], dic_val[4], dic_val[5], dic_val[6], dic_val[7], dic_val[8], dic_val[9], dic_val[10], dic_val[11]
            ,bgtime, edtime)
            excuete_sql = db_insert_sql % value_tuple
            print(excuete_sql)
            g_msql_inst.db_excuete(excuete_sql)
            value_tuple_list.append(value_tuple)
        print("store_cpjlrckh_statics all finish.")

